package com.zhuiyi.yiqingbackend.controller;


import com.zhuiyi.yiqingbackend.common.BaseResponse;
import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.common.ResultUtils;
import com.zhuiyi.yiqingbackend.domain.RecordUserTable;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.service.RecordUserTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/recordUserTable")
@Api(value = "recordUserTable", tags = "展示用户记录")
@CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
public class RecordUserTableController {
    @Resource
    private RecordUserTableService recordUserTableService;

    /**
     * 获取所有用户记录api
     *
     * @return
     */
    @GetMapping("/getall")
    @ApiOperation(value = "all", notes = "获取所有记录")
    public BaseResponse getall() {
        return new BaseResponse(20000, recordUserTableService.getRecordUserTables(), "success");
    }

    /**
     * 添加用户记录api
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "add", notes = "添加记录")
    public BaseResponse add(@RequestBody RecordUserTable recordUserTable) {
        boolean result = recordUserTableService.addRecordUserTable(recordUserTable);
        if (result) {
            return new BaseResponse(20000,"添加成功");
        } else {
            return new BaseResponse(20001, "添加失败");
        }
    }


    /**
     * 删除用户记录api by id
     */

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "delete", notes = "删除记录")
    @ApiImplicitParam(name = "id", value = "user id", required = true, paramType = "query")
    public BaseResponse<Boolean> delete(@PathVariable int id) {
        boolean result = recordUserTableService.delereRecordUserTableById(id);
        if (result) {
            return ResultUtils.success(result);
        } else {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
    }

    /**
     * 更新用户记录api
     */
    @PostMapping("/update")
    @ApiOperation(value = "update", notes = "更新记录")
    public BaseResponse update(@RequestBody RecordUserTable recordUserTable) {
        boolean result = recordUserTableService.updateRecordUserTable(recordUserTable);
        return ResultUtils.success(result);
    }

    /**
     * 根据id获取用户记录api
     */
    @GetMapping("/getById/{id}")
    @ApiOperation(value = "getById", notes = "根据id获取记录")
    public BaseResponse getById(@PathVariable int id) {
        RecordUserTable result = recordUserTableService.getRecordUserTableById(id);
        return ResultUtils.success(result);
    }
}
